package com.xk39.myactiviti.Utils;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

import java.util.List;


public class ActivitiUtils {
    
    public static void deployByBpmnAndPng(String bpmnFile,String pngFile,String deployName){
        // 1.获取ProcessEngine对象
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 2.获取RepositoryService进行部署操作
        RepositoryService service = engine.getRepositoryService();
        // 3.使用RepositoryService进行部署操作
        Deployment deploy = service.createDeployment()
                .addClasspathResource(bpmnFile)
                .addClasspathResource(pngFile)
                .name(deployName)
                .deploy(); //部署流程
        // 4.输出流程部署的信息
        System.out.println("deploy.getId() = " + deploy.getId());
        System.out.println("deploy.getName() = " + deploy.getName());
    }
    
    public static void deployByBpmnAndPng(String bpmnFile,String deployName){
        // 1.获取ProcessEngine对象
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 2.获取RepositoryService进行部署操作
        RepositoryService service = engine.getRepositoryService();
        // 3.使用RepositoryService进行部署操作
        Deployment deploy = service.createDeployment()
                .addClasspathResource(bpmnFile)
                .name(deployName)
                .deploy(); //部署流程
        // 4.输出流程部署的信息
        System.out.println("deploy.getId() = " + deploy.getId());
        System.out.println("deploy.getName() = " + deploy.getName());
    }
    
    public static void deleteDeployById(String deploymentId){
        System.out.println("*******普通删除********");
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = engine.getRepositoryService();
        try {
            repositoryService.deleteDeployment(deploymentId);
            System.out.println(deploymentId+" 删除成功!");
        }catch (Exception e){
            System.out.println("删除失败,该部署不存在或存在任务，若继续请检查id或使用级联删除!");
        }
    }
    
    
    public static void deleteAllProcessInstanceByKey(String processDefKey){
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = engine.getRuntimeService();
        
        List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery()
                .processDefinitionKey(processDefKey)
                .list();
        System.out.printf(processDefKey+ " : 现有 %d 个流程实例%n",processInstances.size());
        
        for (ProcessInstance processInstance : processInstances) {
            System.out.println("正在删除 "+processInstance.toString());
            runtimeService.deleteProcessInstance(processInstance.getProcessInstanceId(),processInstance.toString());
        }
        long count = runtimeService.createProcessInstanceQuery()
                .processDefinitionKey(processDefKey)
                .count();
        System.out.printf(processDefKey+ " : 现有 %d 个流程实例%n",count);
    }
    
    public static void completeTask(String processDefKey,String assignee){
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = engine.getTaskService();
        Task task = taskService.createTaskQuery()
                .processDefinitionKey(processDefKey)
                .taskAssignee(assignee)
                .singleResult();
        System.out.println("----------------");
        if(task!=null){
            taskService.complete(task.getId());
            System.out.println("task.getName() = " + task.getName()+" 任务执行完");
        }else {
            
            List<Task> tasks = taskService.createTaskQuery()
                    .processDefinitionKey(processDefKey).list();
            for (Task task1 : tasks) {
                System.out.println(task1.toString());
            }
            System.out.println(processDefKey+" :"+assignee+ " 未找到相关任务");
        }
    }
    
}
